<template>
  <div id="middle">
    <van-dialog v-model="show" title="填写流量码" show-cancel-button @cancel="cancelTap" @confirm="confirmTap">
      <div class="member" v-if="parent_info.member_id != 0">
        <img :src="parent_info.avatar" alt="" />
        <span>ID:{{ parent_info.member_id }}</span>
      </div>
      <van-cell-group>
        <van-field v-model="value" :placeholder="line_code_switch ? '不填写按直推上级关系绑定' : '请输入流量码'" />
      </van-cell-group>
    </van-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      show: true,
      value: "",
      line_code_switch: false,
      parent_info: {
        member_id: 0
      }
    };
  },
  activated() {
    this.value = "";
    this.show = true;
    this.getData();
  },
  methods: {
    getData() {
      $http.post("plugin.task-package.frontend.index.line-code-page", {}, "加载中..").then(response => {
        if (response.result == 1) {
          this.line_code_switch = response.data.line_code_switch;
          this.parent_info = response.data.parent_info;
        }
      });
    },
    cancelTap() {
      this.$router.push(this.fun.getUrl("member", {}));
    },
    confirmTap() {
      $http.post("plugin.task-package.frontend.index.complete-invite-code", { invite_code: this.value }, "加载中..").then(response => {
        if (response.result == 1) {
          let MinUrl = this.fun.getSiteRoot() + "/addons/yun_shop/?menu#/taskPackage/taskPackageIndex?i=" + this.fun.getKeyByI() + "&type=" + this.fun.getTyep() + "&mid=" + this.fun.getKeyByMid();
          window.location.replace(MinUrl);
        } else {
          this.$dialog
            .alert({
              message: response.msg
            })
            .then(() => {
              this.$router.replace(this.fun.getUrl("member", {}));
            });
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.member {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-top: 2rem;

  img {
    width: 3rem;
    height: 3rem;
    border-radius: 100%;
    margin-bottom: 0.325rem;
  }
}
</style>